/**
 * Created by meil01 on 2019.
 */
 import jQuery  from 'jquery'
 import watermark, { removeWatermark } from "../../framework/common/utils/watermark";
var cstAdapter = (function(){
    var me = {};

    me.sendSubjs = [
        "FillDomain",
        "ValidateDomain",
        "GetDomain",
        "ExecuteBusinessLogic",
        "GetFormHtml",
        "WaterMark",
        "SaveDomain"
    ];

    me.recivSubjs = [
        "custom_loaded",
        "custom_page_resize",
        "custom_fill_domain_success",
        "custom_fill_domain_error",
        "custom_validate_domain_error",
        "custom_validate_domain",
        "custom_execute_business_logic_error",
        "custom_execute_business_logic_success",
        "custom_get_domain_error",
        "custom_get_domain_success",
        "custom_get_form_html_success",
        "custom_refresh_domain_success",
        "custom_refresh_text_success",
        "custom_save_domain",
        "custom_close_business_win" //这个在工作流里没找到调用的代码
    ];
    //不用，暂且整理出来
    me.recivSubjsOld = [
        "blank_html_page_get_html",
        "blank_html_page_render_complete",
        "blank_html_page_get_domain_xml",
        "blank_html_page_reset_form_height",
        "blank_html_page_get_form_data",
        "blank_html_page_alert",
        "blank_html_page_open_modify_business",
        "blank_html_page_change_title",
        "blank_html_page_reload_department",
        "third_platform_sign_success",
        "third_platform_before_initiate_success",
        "blank_html_page_reload_BusinessAttachments"
    ];

   

    me.getSubjects = function(){
        return me.recivSubjs.concat(me.sendSubjs);
    };

    me.filterSend = function(data){
        var ff_data = me.$cf.getFFData();
        if(data.subject == "WaterMark"){
            return data;
        }
        if(data.subject == "FillDomain"){
            data.data = {
                domain:{main:ff_data.main,children:ff_data.children},
                contorl:me.$cf.data("c_data"),
                readonly:me.$cf.data("readonly")
            };
            return data;
        }

        if(data.subject == "ValidateDomain"){
            me.cb_valid_domain_suc = data.data.fn_suc;
            me.cb_valid_domain_err = data.data.fn_err;
            
            if(!me.thirdReady){
                if(!ff_data.isff){
                    me.cb_valid_domain_err && me.cb_valid_domain_err("外挂表单未就绪。");
                }else{
                    me.cb_valid_domain_err && me.cb_valid_domain_err("外挂界面未就绪。");
                }
            }
            data.data ={
                domain:{main:ff_data.main,children:ff_data.children},
                contorl:me.$cf.data("c_data"),
                action:data.data.action
            };
            return data;
        }  
        if(data.subject == "SaveDomain"){
            me.cb_save_domain_suc = data.data.fn_suc;
            me.cb_save_domain_err = data.data.fn_err;
            if(!me.thirdReady){
                me.cb_save_domain_err && me.cb_save_domain_err("外挂界面未就绪。");
            }
            data.data ={
                domain:{main:ff_data.main,children:ff_data.children},
                contorl:me.$cf.data("c_data"),
                action:data.data.action
            };
            return data;
        }
        if(data.subject == "ExecuteBusinessLogic"){
            me.cb_execute_business_suc = data.data.fn_suc;
            me.cb_execute_business_err = data.data.fn_err;
            
            if(!me.thirdReady){
                if(!ff_data.isff){
                    me.cb_valid_domain_err && me.cb_valid_domain_err("外挂表单未就绪。");
                }else{
                    me.cb_valid_domain_err && me.cb_valid_domain_err("外挂界面未就绪。");
                }
            }
            data.data ={
                domain:{main:ff_data.main,children:ff_data.children},
                contorl:me.$cf.data("c_data")
            };
            return data;
        }  
        
        if(data.subject == "GetDomain"){
            me.cb_get_domain_suc = data.data.fn_suc;
            me.cb_get_domain_err = data.data.fn_err;
            data.data = null;
            return data;
        }
        if(data.subject == "GetFormHtml"){
            me.cb_get_html = data.data.fn;
            data.data = null;
            return data;
        }
        return data;
    };

    me.filterReceive = function(data,orgin,source){   
        var d = data.data;      
        if(data.subject == "custom_loaded" 
         || data.subject == "custom_page_resize"
         || data.subject == "custom_fill_domain_success") return d.height;
        
        if(data.subject == "custom_fill_domain_error" 
         || data.subject == "custom_validate_domain_error"
         || data.subject == "custom_get_domain_error"
         || data.subject == "custom_refresh_domain_success"
         || data.subject == "custom_refresh_text_success") return d;

        if(data.subject == "custom_get_domain_success"
         || data.subject == "custom_get_form_html_success") return d.data;

        if(data.subject == "custom_validate_domain"){
            return {
                success:d.success,
                errMsg:d.errMsg||""
            }
        }

        return d;
    };
    me.timer = null;
    me.getData = function(subject,data){
        
        if(subject == "custom_loaded"){
            me.iframe.height = data;
            
            //重新设置水印
            removeWatermark(".runtime-form");
            watermark(window.__watermarkContent__, ".runtime-form");
            
            var ff_data = me.$cf.getFFData();
            me.sendData("FillDomain",ff_data);
        }

        if(subject == "custom_page_resize") {
            // me.iframe.height = data; //会变成缓慢增长的动画。。。
            clearTimeout(me.timer);
            me.timer = setTimeout(function(){
                me.iframe.height = data; 
                //重新设置水印
                removeWatermark(".runtime-form");
                watermark(window.__watermarkContent__, ".runtime-form");
            }, 100);
            me.$cf.iframeResize();
         }

         if(subject == "custom_fill_domain_success"){
            me.iframe.height = data;
            
            //重新设置水印
            removeWatermark(".runtime-form");
            watermark(window.__watermarkContent__, ".runtime-form");
            
            me.$cf.iframeDataLoaded(data);
            //这是个标志，标志第三方页面挂接正确，实现了填充业务对象数据的接口并填充成功了；
            //后面表单提交必须以这个标志为前提，否则要提示错误；
            me.thirdReady = true;
            var ff_data = me.$cf.getFFData();
            if(!ff_data.isff){
                if(jQuery.isBelowIE(8,9)){
                    var userName = me.$cf.getCurUserName();
                    me.sendData("WaterMark",userName);
                }
            }
         }

         if(subject == "custom_fill_domain_error") {
            var errMsg = data;
            me.$cf.trigger("ff_ctl_erro",errMsg);
         }

         if(subject == "custom_validate_domain") {
            if(data.success){
                me.cb_valid_domain_suc();
            }else{
                me.cb_valid_domain_err(data.errMsg);
            }
         }
         if(subject == "custom_save_domain") {
            if(data.success){
                me.cb_save_domain_suc();
            }else{
                me.cb_save_domain_err(data.errMsg);
            }
         }
         if(subject == "custom_execute_business_logic_success") {
            if(data.success){
                me.cb_execute_business_suc();
            }else{
                me.cb_execute_business_err(data.errMsg);
            }
         }
         
         if(subject == "custom_execute_business_logic_error"){
            var errMsg = data;
            me.cb_execute_business_err(errMsg);
         }
         if(subject == "custom_validate_domain_error"){
            var errMsg = data;
            me.cb_valid_domain_err(errMsg);
         }

         if(subject == "custom_get_domain_success"){
            var ff_data = data;
            me.$cf.setFFData(ff_data);
            me.cb_get_domain_suc(ff_data);
         }
         if(subject == "custom_get_domain_error"){
            var errMsg = data;
            me.$cf.trigger("ff_ctl_erro",errMsg);
            me.cb_get_domain_err(errMsg);
         }

         
         if(subject == "custom_get_form_html_success"){
            me.cb_get_html(data||"");
         }

         if(subject == "custom_refresh_domain_success"){
            var ff_data = data;
            me.$cf.setFFData(ff_data);
         }
         
         if(subject == "custom_refresh_text_success"){    
            var ff_text = data;
            me.$cf.setFFText(ff_text);
         }


    };
    me.setData = null;
    me.$cf = null; 
    me.iframe = null;
    me.cb_get_domain_suc = null;
    me.cb_get_domain_err = null;
    me.cb_valid_domain_suc = null;
    me.cb_valid_domain_err = null;
    me.cb_execute_business_suc = null;
    me.cb_execute_business_err = null;
    me.cb_get_html = null;
    me.cb_save_domain_suc = null;
    me.cb_save_domain_err = null;

    me.bindFormEvents = function($cf,iframe_id,frmEvts){
        me.$cf = $cf;
        me.iframe = document.getElementById(iframe_id);
        var map = {};
        map[frmEvts.form_loaded] = "FillDomain";
        map[frmEvts.form_check_req] = "ValidateDomain";
        map[frmEvts.execute_business] = "ExecuteBusinessLogic";
        map[frmEvts.get_cf_data] = "GetDomain";
        map[frmEvts.get_cf_html] = "GetFormHtml";
        map[frmEvts.form_save_data] = "SaveDomain";

        var evt_names = [];
        for(var key in map){
            evt_names.push(key);
        }
        $cf.bind(evt_names.join(" "),function(e,edata){
            //这里的eData参数只是表单原始事件的参数，实际要传递的参数可以自己另行构建
            me.sendData(map[e.type],edata || undefined);
        });

    };

    me.sendData = function(subject,data){
        me.setData && me.setData({
            subject:subject,
            data:data
        });
    };
    return me;

})();

export {cstAdapter}